package srzy.module

import java.io.File
import srzy.common.Logging
import srzy.common.Config
import srzy.common._

object ReadCdrFile extends AbsCaseDeal with Logging {

  def deal = {
    logger.debug("read Cdr File")

    //    val cdrDirMap2 = cdrDirMap.toList.map(x => (x._2, x._1)).toMap
    //    val cdrPaths = cdrDirMap2.toList.map(_._1)
    //    val cdrPathFiles = cdrPaths.map(x => (x, new File(x)))
    //    val cdrPathFiles = cdrPaths.map(_._1)

    //    def getCdrNum = cdrPathFiles.map(_.listFiles().size).reduceLeft(_ + _)
    def getCdrNum = cdrPaths.map(_._2.listFiles().size).reduceLeft(_ + _)

    var i = 0

    while (getCdrNum == 0 && i <= 5) {
      Thread.sleep(1000)
      i += 1
      logger.debug("wait for cdr file " + i + " s")
    }

    getCaseInfo.realFullCdrs = cdrPaths.flatMap(x => x._2.listFiles().map(e => (x._1, e)))
      .flatMap(x => srzy.common.readFromFile(x._2).map(e => (x._1, e)))
      .sortWith((e1, e2) => (e1._1 + e1._2) > (e2._1 + e2._2))
    //    val cdrPath = cdrPathFiles.flatMap(x => x._2.listFiles().toList.map(e => (x._1, e)))
    //
    //    if (cdrPath.size > 1) {
    //      throw new Exception("cdr file :" + cdrPath.map(x => (x._1, x._2.getAbsolutePath())).mkString(" ") + " too much")
    //    }

    //    if (cdrPath.size == 1) {
    //      getCaseInfo.cdrInfo.realFull = srzy.common.readFromFile(cdrPath.head._2)
    //      getCaseInfo.caseType = cdrDirMap2.apply(cdrPath.head._1)
    //    }

  }
}